from nodetypes.formats.mml.Events import EventHandler
from nodetypes.core.Preferences import UrlGuestPreferencesUpdater

from myelin.server.lib.http import Status

from forms import Commit
 
from base import EventHandler


class EventHandler(EventHandler):
           
    newPreferencesUpdater   = UrlGuestPreferencesUpdater
    
    newCommitForm           = Commit.Form
        
    def process(self, context, source):
        if context.account.isLoggedIn():
            return self.redirectToUserPreferences(context)
        
        if self.newPreferencesUpdater().doUpdate(context, source):            
            eventData = context.event.data
            eventData.status = u'success'
            eventData.status_type = u'commit-success'
        else:
            eventData = context.event.data
            eventData.status = u'failure'
            eventData.status_type = u'commit-param-errors'
            
            context.request.status = Status.BAD_REQUEST
                
        eventData.form = self.newCommitForm()
       
        eventData.stage = 0
        eventData.submits = context.transform.submitEvents[0]
        
        context.model = context.nodeType.retrieveModel(context)
        
        return self.newDocument().createDocument(context, source)